use "/Users/jrthornton/Desktop/replication/anes_timeseries_cdf_stata_20211118.dta" , clear
set more off

********************************************************************************
*                	        		 Coding       			                   *
********************************************************************************

* Survey year
gen year = VCF0004
drop if year<2012
drop if year>2016

gen y = 0 if year==2012
replace y = 1 if year==2016

* Weighting
svyset [pw=VCF0009z]

* Mode
gen mode = VCF0017
gen internet = 0 if mode == 0
replace internet = 1 if mode==4

gen i2020 = 1 if VCF0017==4 & year==2020
replace i2020 = 0 if VCF0017==3 & year==2020
replace i2020 = 0 if VCF0017==5 & year==2020

* Satisfaction with democracy
gen satisfaction = VCF9254 if year<2002
replace satisfaction = . if satisfaction<0
replace satisfaction = VCF9255 if year>2002
replace satisfaction = . if satisfaction<0
revrs satisfaction , replace

* Binary satisfaction
gen satis_binary = satisfaction
recode satis_binary (1=0) (2=0) (3=1) (4=1)

* Winner
gen vote = VCF0713
recode vote (1=0) (2=1) (3=.) (4=.) (9=.) (0=.)
gen winner=vote
recode winner (1=0) (0=1) if year==2008
recode winner (1=0) (0=1) if year==2012
recode winner (1=0) (0=1) if year==1996
recode winner (1=0) (0=1) if year==2020
label define winner 0 "Loser" 1 "Winner" , replace
label values winner winner 

* Partisanship and party
gen pid = VCF0301
recode pid (0=.)
replace pid = . if pid==9

gen party = pid
recode party (1=1) (2=1) (3=1) (5=0) (6=0) (7=0) (0=.) (4=2)
label define partylabel 0 "Republican" 1 "Democrat"  2 "Independent" , replace
label values party partylabel 

gen repdem = party if party<2
label values repdem partylabel 

* Ideology 
	
* Self ideology (recoded to range -3-3; -2 Havent thought, -8 DK, -9 NA)
gen ideo = VCF0803
replace ideo = . if ideo < 1 
replace ideo = . if ideo == 9 

* Education
gen education = VCF0110
replace edu = . if educ==0

* Race
gen race = VCF0105b
recode race (0=.) (9=.)
label define racelabel 1 "White" 2 "Black" 3 "Hispanic" 4 "Other"  , replace
label values race racelabel  
*1. White non-Hispanic
*2. Black non-Hispanic
*3. Hispanic
*4. Other or multiple races, non-Hispanic
gen black = 0 if race==1

* Age
gen age = VCF0101
replace age = 97 if age>97
replace age = . if age==0

* Sex (female)
gen female = VCF0104
replace female = . if fem ==0
replace female = . if fem ==3
replace female = fem-1
label define femalelabel 0 "Male" 1 "Female" , replace
label values female femalelabel  

* Income
gen income = VCF0114
replace income = . if income == 0

* Region 
gen region = VCF0112
label define region 1 "Northeast" 2 "Midwest" 3 "South" 4 "West" , replace
label values region region  

* Marital status
gen married =  VCF0147 if  VCF0147 < 8
recode married (7=6)
label define marriedlabel 1 "Married" 2 "Never married" 3 "Divorced" ///
	4 "Separated" 5 "Widowed" 6 "Partners" , replace 
label values married marriedlabel  

* Interest
gen interest = VCF0310
recode interest (9=.) (0=.)

* Economic perceptions
gen natl_econ = VCF0871*-1+6

* Alaska/Hawaii
gen ALHI = 1 if VCF0901a == 15
replace ALHI = 1 if VCF0901a == 2
replace ALHI = 0 if ALHI!=1

********************************************************************************
*                                   Analyses                                   *
********************************************************************************

*****************
* Main analyses *
*****************
svy: reg satis_binary internet age income female i.married i.region i.race ///
	edu if year == 2012
svy: reg satis_binary internet age income female i.married i.region i.race ///
	edu if year == 2016
svy: reg satis_binary internet age income female i.married i.region i.race ///
	edu i.y 
	
****************
* Balance test *
****************
svy: logit internet age income female i.married i.region i.race edu ///
	if year == 2012
svy: logit internet age income female i.married i.region i.race edu ///
	if year == 2016


***********************
* Logistic regression *
***********************
	
svy: logit satis_binary internet age income female i.married i.region i.race ///
	edu if year == 2012
margins , at(internet=(0 1)) post
mlincom 2-1

svy: logit satis_binary internet age income female i.married i.region i.race ///
	edu if year == 2016
margins , at(internet=(0 1)) post
mlincom 2-1

svy: logit satis_binary internet age income female i.married i.region i.race ///
	edu i.y 
margins , at(internet=(0 1)) post
mlincom 2-1


*****************************
* Without Alaska and Hawaii *
*****************************
svy: reg satis_binary internet age income female i.married i.region i.race ///
	edu if year == 2012 & ALHI==0
svy: reg satis_binary internet age income female i.married i.region i.race ///
	edu if year == 2016 & ALHI==0
svy: reg satis_binary internet age income female i.married i.region i.race ///
	edu i.y if ALHI==0

**************
* Unweighted *
**************
* Unweighted replication of main anlyses
reg satis_binary internet age income female i.married i.region i.race edu ///
	if year == 2012
reg satis_binary internet age income female i.married i.region i.race edu ///
	if year == 2016
reg satis_binary internet age income female i.married i.region i.race edu i.y

* Unweighted balance test
logit internet age income female i.married i.region i.race edu if year == 2012
logit internet age income female i.married i.region i.race edu if year == 2016

* Figures E1
logit internet age income female i.married i.region i.race edu if year == 2012
margins , at(age=(17 20(10)90))
margins , at(married=(1(1)6)) post
# delimit ;
	coefplot  
	,
	vert 
	msymbol(O) mcolor(black) lcolor(black)
	aspect(.9) 
	xlab(,angle(90)) 
	ylab(.5 "0.50" .55 "0.55" .6 "0.60" .65 "0.65" .7 "0.70") 
	xlab(1 "Married" 
		2 "Never married" 
		3 "Divorced" 
		4 "Separated" 
		5 "Widowed" 
		6 "Partners")
	xtitle(" ") 
	ytitle("Pr(Internet)")
	;
# delimit cr



***********
* Ordinal *
***********
svy: reg satisfaction internet age income female i.married i.region i.race ///
	edu if year == 2012
svy: reg satisfaction internet age income female i.married i.region i.race ///
	edu if year == 2016
svy: reg satisfaction internet age income female i.married i.region i.race ///
	edu i.y 

svy: ologit satisfaction internet age income female i.married i.region edu ///
	if year == 2012
margins , at(internet=(0 1)) post
svy: ologit satisfaction internet age income female i.married i.region edu ///
	if year == 2016
margins , at(internet=(0 1)) post
svy: ologit satisfaction internet age income female i.married i.region i.race ///
	edu i.y 
margins , at(internet=(0 1)) post
	
********************************************************
* Adjusting for partisanhip, ideology, and vote choice *
********************************************************

svy: reg satis_binary internet age income female i.married i.region i.race ///
	edu winner pid ideo if year == 2012	
svy: reg satis_binary internet age income female i.married i.region i.race ///
	edu winner pid ideo if year == 2016	
svy: reg satis_binary internet age income female i.married i.region i.race ///
	edu winner pid ideo i.y

	
********************************************************************************
*                                   Extensions                                 *
********************************************************************************

*******************
* Alternative DVs *
*******************
gen power = VCF9253
recode power (-9=.) (-8=.)
gen difference_vote = VCF9250
recode difference_vote (-9=.) (-8=.)

gen trust = VCF0605
recode trust (1=0) (2=1) (0=.) (9=.)
label define trustlabel 0 "0. A few big interests" 1 "1. Benefit of all"
label values trust trustlabel  

alpha power difference , gen(efficacy)
	replace efficacy = . if power == .
	replace efficacy = . if difference == .

svy: reg trust internet age income female i.married i.region i.race ///
	edu if year == 2012
svy: reg trust internet age income female i.married i.region i.race ///
	edu if year == 2016
svy: reg trust internet age income female i.married i.region i.race ///
	edu i.y
	
svy: reg efficacy internet age income female i.married i.region i.race ///
	edu if year == 2012
svy: reg efficacy internet age income female i.married i.region i.race ///
	edu if year == 2016
svy: reg efficacy internet age income female i.married i.region i.race ///
	edu i.y

**********************************************************************
* "Traditional model" with interaction between winner/loser and mode *
**********************************************************************
svy: reg satis_binary interest natl_econ ideo income age educ female ///
	winner##internet if year == 2012	
svy: reg satis_binary interest natl_econ ideo income age educ female ///
	winner##internet if year == 2016
svy: reg satis_binary interest natl_econ ideo income age educ female ///
	winner##internet i.y

	
**********************************************************************
* "Traditional model" with interaction between winner/loser and mode *
**********************************************************************
logit internet age income female i.married i.region i.race edu if year == 2016
margins , at(age=17 20(20)90)
use "/Users/jrthornton/Desktop/replication/post_age_balance.dta", clear
 
 # delimit ;	
	twoway 
	(line p age , color(black)) (
	line lower age , lcolor(black) lpattern(dash)) 
	(line upper age , lcolor(black) lpattern(dash)) 
	, 
	legend(off)
	aspect(1) 
	ylab(.5 "0.5" .6 "0.6" .7 "0.7" .8 "0.8" .9 "0.9") 
	xlab(20(10)90)
	xtitle("Age") 
	ytitle("Pr(Internet)")
	;
# delimit cr
